package ModelLayer;

import java.util.ArrayList;

/**
 * Write a description of class DVDContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVDContainer
{
    private ArrayList<DVD> dvdCollection;
    private ArrayList<Copy> copyCollection;

    //Singleton instance
    private static DVDContainer instance;

    /**
     * Constructor for objects of class DVDContainer
     */
    public DVDContainer() //Vi kan lave denne private 
    {
        dvdCollection = new ArrayList<DVD>();
        copyCollection = new ArrayList<Copy> ();
    }
    /**
     * returns instance(Singleton)
     * @returns DVDContainer instance
       */
    public static DVDContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DVDContainer();
        }
        return instance;
    }
    /**
     * adds DVD to collection
     * @param DVD newDvd
       */
    public void addDVD(DVD newDvd)
    {
        dvdCollection.add(newDvd);
    }
    /**
     * returns DVD thisDVD
     * @param long id
     * @return DVD thisDVD
       */
    public DVD getDVD(long id)
    {
        boolean found = false;
        int index = 0;
        DVD thisDVD = null;
        while(!found && index < dvdCollection.size())
        {
            thisDVD = dvdCollection.get(index); // gets the container
            if(thisDVD.getId() == id)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }

        if(found)
        {
            return thisDVD;
        }
        else
        {
            return null;
        }

    }
    /**
     * deletes dvd
     * @param long id
       */
    public void deleteDVD(long id)
    {
        DVD d = getDVD(id);
        dvdCollection.remove(d);
    }
    /**
     * lists all dvds
       */
    public void listAllDvd()
    {
        for(DVD d: dvdCollection){
            System.out.println("Title: "+ d.getTitle() + "\t" + "Artist: "+d.getArtist() +"\t" + "ID: " + d.getId());
        }
    }
    /**
     * returns DVD as updated
     * @param long id
     * @return DVD d
       */
    public DVD updateDvd(long id)
    {
        DVD d = null;

        for(DVD dv : dvdCollection)
        {
            if(dv.getId() == id)
            {
                d = dv;
            }
        }

        return d;
    }

    /**
     * adds Copy to collection
     * @param Copy newCopy
       */
    public void addCopy(Copy newCopy)
    {
        copyCollection.add(newCopy);
    }
    /**
     * Lists all copies
       */
    public void listAllCopy()
    {
        for(Copy c : copyCollection){
            System.out.println("Serial: "+ c.getSerial() + "\t" + "Date: "+ c.getDate() +"\t" + "Price: " + c.getPrice());
        }
    }
    /**
     * return Copy thisCopy
     * @return Copy thisCopy
     * @param long sn
       */
    public Copy getCopy(long sn)
    {
        boolean found = false;
        int index = 0;
        Copy thisCopy = null;
        while(!found && index < copyCollection.size())
        {
            thisCopy = copyCollection.get(index); // gets the container
            if(thisCopy.getSerial() == sn)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }

        if(found)
        {
            return thisCopy;
        }
        else
        {
            return null;
        }

    }
    /**
     * Deletes copy
     * @param long id
       */
    public void deleteCopy(long sn)
    {
        Copy c = getCopy(sn);
        copyCollection.remove(c);
    }
    /**
     * returns arrayList of copies
     * @return ArrayList<Copy> tempCollection
       */
    public ArrayList<Copy> getCopyCollection()
    {
        ArrayList<Copy> tempCollection = copyCollection;
        return tempCollection;
    }
}
